package com.cloudcreativity.blyg.base;

import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.cloudcreativity.blyg.sqlite.AreaWrapper;
import com.cloudcreativity.blyg.sqlite.CityWrapper;
import com.cloudcreativity.blyg.sqlite.ProvinceWrapper;
import com.cloudcreativity.blyg.sqlite.utils.AreaDaoUtils;
import com.cloudcreativity.blyg.sqlite.utils.DaoManager;
import com.cloudcreativity.blyg.utils.LogUtils;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class BaseApp extends Application {

    public static BaseApp app;
    private List<Activity> activities = new ArrayList<>();
    public void addActivity(Activity activity){
        this.activities.add(activity);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        initRefreshLayout();
        // initial green dao
         initGreenDao();

         initAreaData();
    }

    private void initAreaData(){
        //说明省市区的数据没有添加，添加一次
        if(!new AreaDaoUtils(app).isExists()){
            LogUtils.e("xuxiwu","省市区数据不存在");
            //添加省数据
            new Thread(new Runnable() {
                @Override
                public void run() {
                    StringBuilder builder = new StringBuilder();
                    InputStream inputStream;
                    try {
                        inputStream = getResources().getAssets().open("province.json");
                        InputStreamReader reader = new InputStreamReader(inputStream);
                        BufferedReader bReader = new BufferedReader(reader);
                        String jsonLine;
                        while((jsonLine = bReader.readLine())!=null){
                            builder.append(jsonLine);
                        }
                        bReader.close();
                        reader.close();
                        inputStream.close();

                        //解析数据准备添加
                        ProvinceWrapper wrapper = new Gson().fromJson(builder.toString(), ProvinceWrapper.class);
                        new AreaDaoUtils(app).insertProvinces(wrapper.getRecords());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            //添加市数据
            new Thread(new Runnable() {
                @Override
                public void run() {
                    StringBuilder builder = new StringBuilder();
                    InputStream inputStream;
                    try {
                        inputStream = getResources().getAssets().open("city.json");
                        InputStreamReader reader = new InputStreamReader(inputStream);
                        BufferedReader bReader = new BufferedReader(reader);
                        String jsonLine;
                        while((jsonLine = bReader.readLine())!=null){
                            builder.append(jsonLine);
                        }
                        bReader.close();
                        reader.close();
                        inputStream.close();

                        //解析数据准备添加
                        CityWrapper wrapper = new Gson().fromJson(builder.toString(), CityWrapper.class);
                        new AreaDaoUtils(app).insertCities(wrapper.getRecords());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            //添加区县数据
            new Thread(new Runnable() {
                @Override
                public void run() {
                    StringBuilder builder = new StringBuilder();
                    InputStream inputStream;
                    try {
                        inputStream = getResources().getAssets().open("area.json");
                        InputStreamReader reader = new InputStreamReader(inputStream);
                        BufferedReader bReader = new BufferedReader(reader);
                        String jsonLine;
                        while((jsonLine = bReader.readLine())!=null){
                            builder.append(jsonLine);
                        }
                        bReader.close();
                        reader.close();
                        inputStream.close();

                        //解析数据准备添加
                        AreaWrapper wrapper = new Gson().fromJson(builder.toString(), AreaWrapper.class);
                        new AreaDaoUtils(app).insertAreas(wrapper.getRecords());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }else{
            LogUtils.e("xuxiwu","省市区数据已存在");
        }
    }

    private void initGreenDao(){
        DaoManager manager = DaoManager.getInstance();
        manager.init(app);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public void destroyActivity(){
        //结束所有的Activity
        for(Activity activity:activities){
            if(activity!=null&&!activity.isDestroyed())
                activity.finish();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onTerminate() {
        super.onTerminate();
        destroyActivity();
        //onDestroy
        System.exit(0);
    }

    //初始化刷新加载布局
    private void initRefreshLayout(){
        TwinklingRefreshLayout.setDefaultHeader("com.cloudcreativity.blyg.view.ProgressLayout");
        TwinklingRefreshLayout.setDefaultFooter("com.lcodecore.tkrefreshlayout.footer.LoadingView");
    }
}
